<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class User extends Base
{


    public function index()
    {
        return view();
    }

    public function lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        if (empty($post['title']) || $post['title'] == '') {
        } else {
            $map[] = ['title', 'like', '%' . $post['title'] . '%'];
        }
        $data['count'] = Db::name('user')
            ->where($map)
            ->count();
        $data['data'] = Db::name('user')
            ->where($map)
            // ->fetchSql(true)
            ->order('id desc')->page(input("param.page"), input('param.limit'))->select();

        $noun = config('param.noun');
        $tesk_ok = config('param.test_ok');
        $city = config('param.city');
        $sub_city = config('param.sub_city');
        $sex = config('param.sex');
        foreach ($data['data'] as &$info) {
            $info['noun'] = $noun[$info['noun']];
            $info['sex'] = $sex[$info['sex']];
            $info['sub_city'] = $sub_city[$info['sub_city']];
            $info['city'] = $city[$info['city']];
            $info['test_1_status'] = $tesk_ok[$info['test_1_status']];
            $info['test_2_status'] = $tesk_ok[$info['test_2_status']];
            $info['igm'] = $tesk_ok[$info['igm']];
            $info['igg'] = $tesk_ok[$info['igg']];
            $info['c_time'] = date('Y-m-d  H:i:s', $info['c_time']);
            $info['u_time'] = date('Y-m-d  H:i:s', $info['u_time']);
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function status()
    {
        $this->assign('yes_no', config('param.yes_no'));
        $this->assign('noun', config('param.noun'));
        $this->assign('isolate_status', config('param.isolate_status'));
        $this->assign('move_status', config('param.move_status'));
        $this->assign('test_ok', config('param.test_ok'));
        $this->assign('job', config('param.job'));
        $this->assign('city', config('param.city'));
        $this->assign('sub_city', config('param.sub_city'));
        $this->assign('sex', config('param.sex'));
    }

    public function add()
    {
        $post = $this->request->param();
        if ($this->request->isPost()) {

            $validate = new \app\admin\validate\User();

            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            $post['u_time'] = time();
            if ($post['visa_time']) {
                $post['visa_time'] = strtotime( $post['visa_time']);
            }
            if ($post['medical_time']) {
                $post['medical_time'] = strtotime( $post['medical_time']);
            }
            if ($post['move_time']) {
                $post['move_time'] = strtotime( $post['move_time']);
            }
            if ($post['test_1_time']) {
                $post['test_1_time'] = strtotime( $post['test_1_time']);
            }
            if ($post['test_1_ok_time']) {
                $post['test_1_ok_time'] = strtotime( $post['test_1_ok_time']);
            }
            if ($post['test_2_time']) {
                $post['test_2_time'] = strtotime( $post['test_2_time']);
            }
            if ($post['test_2_ok_time']) {
                $post['test_2_ok_time'] = strtotime( $post['test_2_ok_time']);
            }
            if ($post['test_3_time']) {
                $post['test_3_time'] = strtotime( $post['test_3_time']);
            }
            if ($post['test_3_ok_time']) {
                $post['test_3_ok_time'] = strtotime( $post['test_3_ok_time']);
            }
            if ($post['free_isolate_time']) {
                $post['free_isolate_time'] = strtotime( $post['free_isolate_time']);
            }


            if ($post['id']) {
                Db::name('user')->where('id', $post['id'])->update($post);
            } else {
                $post['c_time'] = time();
                $post['uid'] = session('uid');
                Db::name('user')->strict(false)->insert($post);
            }
            Db::startTrans();
            try {

                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        } else {
            $this->status();
            if (isset($post['id']) && !empty($post['id'])) {
                $info = DB::name('user')->where('id', $post['id'])->find();
                if ($info['visa_time'] == 0) {
                    unset($info['visa_time']);
                } else {
                    $info['visa_time'] = date('Y-m-d', $info['visa_time']);
                }
                if ($info['medical_time'] == 0) {
                    unset($info['medical_time']);
                } else {
                    $info['medical_time'] = date('Y-m-d', $info['medical_time']);

                }
                if ($info['move_time'] == 0) {
                    unset($info['move_time']);
                } else {
                    $info['move_time'] = date('Y-m-d', $info['move_time']);

                }
                if ($info['test_1_time'] == 0) {
                    unset($info['test_1_time']);
                } else {
                    $info['test_1_time'] = date('Y-m-d', $info['test_1_time']);

                }
                if ($info['test_1_ok_time'] == 0) {
                    unset($info['test_1_ok_time']);
                } else {
                    $info['test_1_ok_time'] = date('Y-m-d', $info['test_1_ok_time']);

                }
                if ($info['test_2_time'] == 0) {
                    unset($info['test_2_time']);
                } else {
                    $info['test_2_time'] = date('Y-m-d', $info['test_2_time']);

                }
                if ($info['test_2_ok_time'] == 0) {
                    unset($info['test_2_ok_time']);
                } else {
                    $info['test_2_ok_time'] = date('Y-m-d', $info['test_2_ok_time']);

                }
                if ($info['test_3_time'] == 0) {
                    unset($info['test_3_time']);
                } else {
                    $info['test_3_time'] = date('Y-m-d', $info['test_3_time']);

                }
                if ($info['test_3_ok_time'] == 0) {
                    unset($info['test_3_ok_time']);
                } else {
                    $info['test_3_ok_time'] = date('Y-m-d', $info['test_3_ok_time']);
                }
                if ($info['free_isolate_time'] == 0) {
                    unset($info['free_isolate_time']);
                } else {
                    $info['free_isolate_time'] = date('Y-m-d', $info['free_isolate_time']);
                }

                $info['c_time'] = date('Y-m-d  H:i:s', $info['c_time']);
                $info['u_time'] = date('Y-m-d  H:i:s', $info['u_time']);
                $this->assign('info', $info);
            }


            return view();
        }
    }


    public function helpdelete()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['id'])) {
                $this->error('请求参数错误');
            }

            Db::startTrans();
            try {
                $info = Db::name('help')->where(['id' => $post['id']])->delete();
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }
    }

}